<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Controller;
use think\Request;
use think\Db;
use think\db\Query;

class SchoolController extends AdminBaseController
{
    //显示
    public function index()
    {
        //搜索条件
        $name = $this->request->param('name');
        //查询表字段
        $school = Db::name('school')
            //接收所查询的值,使用where的闭包函数查询
            ->where(function(Query $query) use ($name){
                //学校查询
                if($name){
                    $query->where('name','like',"%$name%");
                }
            })
            //倒序排列
            ->order("id DESC")
            //分页：每页条数，跳转路径，额外参数
            ->paginate(3, false, ['query' => $this->request->param()]);
        //渲染分页
        $page = $school->render();
        //渲染页面，传参 
        return view('index',['school' => $school,'page' => $page]);
    }

     //添加
     public function add()
     {
         //查询
         $province = Db::name('area')
            ->where('level',1)
            ->select();
        //判断
        if ($this->request->isPost()) {
            $add = Db::name('school') -> insert(input());
            if ($add) {
                $this -> success("添加成功", url("school/index"));
            } else {
                $this -> error("添加失败");
            }
        }
         //渲染页面
         return view('school_add',['province' => $province]);
     }

     //获取城市
     public function getCity()
     {
         $areaId = input('areaId');
         $city = Db::name('area')
            ->where('parentId', $areaId)
            ->where('level', 2)
            ->select();
        $cityArr = [
            "<option value=' '>-请选择城市-</option>"
        ];
        foreach ($city as $v) {
            $opt = "<option value = '".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($cityArr, $opt);
        }
        return $cityArr;
     }

     //获取县区
     public function getArea()
     {
         $areaId = input('areaId');
         $area = Db::name('area')
            ->where('parentId', $areaId)
            ->where('level', 3)
            ->select();
        $areaArr = [
            "<option value=' '>-请选择城市-</option>"
        ];
        foreach ($area as $v) {
            $opt = "<option value = '".$v['areaId']."'>".$v['areaName']."</option>";
            array_push($areaArr, $opt);
        }
        return $areaArr;
     }
 
     //编辑
     public function edit()
     {
         //查询
         $province = Db::name('area')
            ->where('level',1)
            ->select();
         //判断
         if (request()->isPost()){
             //获取当前id对应的全部数据
             $data = input();
             //查询指定id的数据
             $where = ['id'=>$data['id']];
             //更新指定id的数据
             $edit =  Db::name('school')->update($data,$where);
             //判断是否更新成功
             if ($edit){
                 $this->success('修改成功','school/index');
             }else{
                 $this->error('修改失败');
             }
         }
         //查询要编辑的记录
         $id = input('id');
         $school =  Db::name('school')->get($id);
         //渲染页面，传参
         return $this -> fetch('school_edit',[
             'school' => $school,
             'province' => $province]);
     }
 
     //删除
     public function delete()
     {
         //获取当前id对应的全部数据
         $data=input();
         //获取数据库中的id，删除数据
         $del= Db::name('school')->delete($data['id']);
         //判断是否删除成功
         if($del){
             $this->success('删除成功','school/index');
         }else{
             $this->error('删除失败');
         }
     }
}